import { defineStore } from "pinia";

import { POST } from "@/utils";

import device from '@/assets/img/TestTube.png'

import { useCommon } from "./Common";
const CommonStore = useCommon()
const getTestTubeTraceApi = (data: any) => POST('/queryPutHistory', data, false)

export const useTestTubeTrace = defineStore('testTubeTrace', {
    state: (): TestTubeTrace => {
        return {
            visible1: false,
            visible2: false,
            searchForm: {
                putInfo: '',
                department: '',
                pageNo: 1,
                pageSize: 20,
            },
            traceArr: [],
            data: {}
        }
    },
    actions: {
        async getTestTubeTrace() {
            this.traceArr = []
            if (CommonStore.department != '检验科') {
                this.searchForm.department = CommonStore.department
            }
            let record = []
            const data = await getTestTubeTraceApi(this.searchForm) as any
            if (data) {
                data[0].forEach((item: any) => {
                    if (item.outData) {
                        record = [
                            {
                                title: '放样',
                                Time: item.putData,
                                deviceName: item.deviceName,
                                serial: item.serial,
                                src: item.putUrl,
                                visible: this.visible1,
                                size: 100
                            },
                            {
                                title: "取样",
                                Time: item.outData,
                                name: item.name,
                                sex: item.sex,
                                department: item.department,
                                src: item.outUrl,
                                visible: this.visible2,
                                size: 100
                            }
                        ]
                    } else {
                        record = [{
                            title: '放样',
                            Time: item.putData,
                            deviceName: item.deviceName,
                            serial: item.serial,
                            src: item.putUrl,
                            visible: this.visible1,
                            size: 100
                        }]
                    }
                    this.traceArr.push(record)
                })


                // if (data[0][0].outData) {
                //     this.traceArr = [
                //         {
                //             title: '放样',
                //             Time: data[0][0].putData,
                //             deviceName: data[0][0].deviceName,
                //             serial: data[0][0].serial,
                //             src: data[0][0].putUrl,
                //             visible: this.visible1,
                //             size: 100
                //         },
                //         {
                //             title: "取样",
                //             Time: data[0][0].outData,
                //             name: data[0][0].name,
                //             sex: data[0][0].sex,
                //             department: data[0][0].department,
                //             src: data[0][0].outUrl,
                //             visible: this.visible2,
                //             size: 100
                //         }
                //     ]
                // } else {
                //     this.traceArr = [
                //         {
                //             title: '放样',
                //             // title: `放样\n放样时间：${data[0][0].putData}\n放样设备：${data[0][0].deviceName}\n序列号：${data[0][0].serial}`,
                //             Time: data[0][0].putData,
                //             deviceName: data[0][0].deviceName,
                //             serial: data[0][0].serial,
                //             src: data[0][0].putUrl.replace('localhost:10000', '192.168.31.17:8546'),
                //             visible: this.visible1,
                //             size: 100
                //         }
                //     ]
                // }
                this.data = {
                    title: this.searchForm.putInfo,
                    type: 'img',
                    size: 100,
                    src: device,
                    children: this.traceArr
                }
            } else {
                this.traceArr = []
                this.data = {}
            }
        },
        changeVisible(type: string) {

            if (type == 'child1') {
                this.data.children[0].visible = !this.data.children[0].visible
            } else {
                this.data.children[1].visible = !this.data.children[1].visible
            }

        },
    }
})
